import { getQiniuTokenApi, toQiniuApi } from "@/api/common/common.js"
import { ElMessage } from "element-plus"
export const deleteInputFile = () => {
  document.getElementById("inputFile") &&
    document.getElementById("inputFile").parentNode.removeChild(document.getElementById("inputFile"))
}
export const uploadFile = (size, accept) => {
  return new Promise((resolve, reject) => {
    deleteInputFile()
    const inputFile = document.createElement("input")
    inputFile.type = "file"
    inputFile.id = "inputFile"
    inputFile.accept = accept
    document.body.appendChild(inputFile)
    inputFile.click()
    inputFile.addEventListener("change", (e) => {
      const file = e.target.files[0]
      console.log(file, "文件文件")
      const formdata = new FormData()
      formdata.append("file", file)
      if (size && file.size > size * 1024 * 1024) {
        ElMessage.warning(`文件大小不能超过${size}M`)
        reject(`文件大小不能超过${size}M`)
      } else {
        getQiniuTokenApi()
          .then((data) => {
            const upToken = data.content.upToken
            const domain = data.content.domain
            formdata.append("token", upToken)
            toQiniuApi(formdata)
              .then((data) => {
                let url = ""
                if (domain.indexOf("http") !== -1) {
                  url = domain + "/" + data.key
                } else {
                  url = "https://" + domain + "/" + data.key
                }
                const result = {
                  url: url,
                  name: file.name,
                  size: file.size,
                  type: file.type
                }
                deleteInputFile()
                resolve(result)
              })
              .catch((err) => {
                console.log(err)
              })
          })
          .catch((err) => {
            console.log(err)
          })
      }
    })
  })
}
// 多文件上传，返回文件数组
export const uploadFileMul = (size, accept, multiple) => {
  return new Promise(function (resolve, reject) {
    deleteInputFile()
    const inputFile = document.createElement("input")
    const pros = []
    inputFile.type = "file"
    inputFile.id = "inputFile"
    inputFile.multiple = multiple
    inputFile.accept = accept
    document.body.appendChild(inputFile)
    inputFile.click()
    inputFile.addEventListener("change", (e) => {
      const fileList = e.target.files
      const files = Array.prototype.slice.call(fileList)
      files.forEach(function (file) {
        if (size && file.size > size * 1024 * 1024) {
          reject(`文件大小不能超过${size}M`)
        } else {
          const formdata = new FormData()
          formdata.append("file", file)
          pros.push(toQiniuHandle(formdata, file))
        }
      })
      Promise.all(pros)
        .then(function (res) {
          // console.log(res,'上传文件')
          resolve(res)
        })
        .catch(function (reason) {
          console.log(reason)
          reject(reason)
        })
    })
  })
}

export const toQiniuHandle = (formdata, file, attFlag) => {
  return new Promise(function (resolve, reject) {
    getQiniuTokenApi().then((res) => {
      const upToken = res.content.upToken
      const domain = res.content.domain
      formdata.append("token", upToken)
      toQiniuApi(formdata)
        .then((data) => {
          let url = ""
          if (domain.indexOf("http") !== -1) {
            url = domain + "/" + data.key
          } else {
            url = "https://" + domain + "/" + data.key
          }
          const result = {
            fileUrl: attFlag ? url + "?attname=" + file.name : url,
            fileName: file.name,
            size: file.size,
            type: file.type
          }
          deleteInputFile()
          resolve(result)
        })
        .catch((err) => {
          console.error(err)
          reject(err)
        })
    })
  })
}

export const uploadImage = (size) => {
  return new Promise((resolve, reject) => {
    deleteInputFile()
    const inputFile = document.createElement("input")
    inputFile.type = "file"
    inputFile.id = "inputFile"
    inputFile.accept = "image/*"
    document.body.appendChild(inputFile)
    inputFile.click()
    inputFile.addEventListener("change", (e) => {
      const file = e.target.files[0]
      const formdata = new FormData()
      formdata.append("file", file)
      if (size && file.size > size * 1024 * 1024) {
        reject(`文件大小不能超过${size}M`)
      } else {
        getQiniuTokenApi()
          .then((res) => {
            const data = res.data
            if (data.code === "000") {
              const upToken = data.content.upToken
              const domain = data.content.domain
              formdata.append("token", upToken)
              toQiniuApi(formdata)
                .then((res) => {
                  const data = res.data
                  let url = ""
                  if (domain.indexOf("http") !== -1) {
                    url = domain + "/" + data.key
                  } else {
                    url = "https://" + domain + "/" + data.key
                  }
                  deleteInputFile()
                  resolve(url)
                })
                .catch((err) => {
                  console.log(err)
                })
            }
          })
          .catch((err) => {
            console.log(err)
          })
      }
    })
  })
}
